package com.qinsoft.frame.taobao.title;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import com.qinsoft.frame.exception.ApiException;
import com.qinsoft.frame.messages.ResultMessage;
import com.qinsoft.frame.utils.TaobaoInter;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemUpdateResponse;
import com.taobao.api.response.ItemcatsGetResponse;
import com.taobao.api.response.ItemsOnsaleGetResponse;


public class TitleService {
    
    /**
     * 修改全店商品
     */
    @Test
    public void updateAll() throws ApiException{
        ResultMessage result = new ResultMessage();
        String tql = "select num_iid,cid from taobao.items.onsale.get where page_size=200";
        List<Item> items = TaobaoInter.sendInter(tql, ItemsOnsaleGetResponse.class).getItems();
        for(Item item : items){
            try
            {
                update(item.getNumIid());
            }
            catch (ApiException e)
            {
                result.addException(e);
            }
        }
    }
    
    /**
     * 修改单个商品
     */
	public ResultMessage update(Long numIid) throws ApiException{
        String parent = "";
        String parentCat = "";
        String child = "";
        String childCat = "";
        List<String> propName = new ArrayList<String>();
        ResultMessage result = new ResultMessage();
        
        String tql = "select num_iid,title,props_name,cid from item where num_iid="+numIid;
        Item item = TaobaoInter.sendInter(tql, ItemGetResponse.class).getItem();
        //获取类目
        tql = "select parent_cid,is_parent,name from taobao.itemcats.get where cids="+item.getCid();
        ItemCat itemCat = TaobaoInter.sendInter(tql, ItemcatsGetResponse.class).getItemCats().get(0);
        childCat = itemCat.getName();
        
        //获取父类目
        if(!itemCat.getIsParent()){
            tql = "select parent_cid,is_parent,name from taobao.itemcats.get where cids="+itemCat.getParentCid();
            itemCat = TaobaoInter.sendInter(tql, ItemcatsGetResponse.class).getItemCats().get(0);    
        }
        parentCat = itemCat.getName();
        
        //获取属性
        String props = item.getPropsName();
        List<String> list = new ArrayList<String>(Arrays.asList(props.split(";")));

        for(String nature : list){
            if(nature.indexOf("(") != -1){
                nature = nature.substring(0, nature.indexOf("("));
            }
            String name = nature.split(":")[3];
            if(parent.length()<15 && (nature.startsWith("10537956") || nature.startsWith("33243"))){
                parent += name.trim();
            }
            else if(name.matches("[^0-9](.)*")){
                propName.add(name);
            }
        }
        //随机获取属性
        Collections.shuffle(propName);
        
        //组合标题
        for(String itemTitle : propName){
            if(length(child) <= (30-length(parentCat)-length(childCat)-length(parent)-length(itemTitle))){
                child += itemTitle;
            }
        }
        String title = parent+parentCat+child+childCat;
        System.out.println("修改后的标题:"+title+"--长度:"+length(title));
        try
        {
            //修改标题
            tql = "update taobao.item.update set title="+title+" where num_iid="+item.getNumIid();
            TaobaoInter.sendInter(tql, ItemUpdateResponse.class);
        }
        catch (ApiException e)
        {
            result.addException(e);
        }
        result.setResult(title);
        return result;
	}
    
    /**
     * 获取字符串的长度，中文占一个字符,英文数字占半个字符
     */
    public static int length(String value){
        double valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < value.length(); i++) {
            String temp = value.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 1;
            } else {
                valueLength += 0.5;
            }
        }
        return  (int) Math.ceil(valueLength);
    }
}
